const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    user: {
      avatar_url: defaultAvatarUrl,
      nick_name: '',
      open_id: '',
      user_type: ''
    },
    show: false,
    user_show: false
  },
  //获取头像
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail

    this.data.user.avatar_url = avatarUrl

    this.setData({
      user: this.data.user
    })

  },
  //分享朋友圈
  onShareAppMessage() {},
//分享好友
  onShareTimeline() {},
  //获取微信昵称
  bindblur(e) {

    this.data.user.nick_name = e.detail.value

    this.setData({
      user: this.data.user,
      
    })

  },

  onLoad: function () {
    this.setData({
      show: false
    })
    const str_user = wx.getStorageSync('user')

    if (str_user) {
      const user = JSON.parse(str_user)

      this.setData({
        user: user,
        user_show: true
      })
    }


  },
  //展示弹窗
  showPopup() {
    this.setData({
      show: true
    });
  },

  //关闭弹窗
  onClose() {
    //清空用户名和头像
    this.data.user.nick_name = ''
    this.data.user.avatar_url = defaultAvatarUrl
    this.setData({
      show: false,
      user: this.data.user
    });
  },
  //确定登录
  formSubmit() {
     //昵称校验
    if (!this.data.user.nick_name) {
     
      wx.showToast({
        title: '请完善昵称',
        icon: 'error',
        duration: 2000
      })

      return false
    }
    //头像校验
    // if (this.data.user.avatar_url === defaultAvatarUrl) {
    //   wx.showToast({
    //     title: '请完善头像',
    //     icon: 'error',
    //     duration: 2000
    //   })

    //   return false
    // }


    //获取open_id
    const that = this

    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'getOpenId',
      }
    }).then(res => {
      this.data.user.open_id = res.result.openid
      that.setData({
        user: that.data.user
      });
      
      wx.cloud.callFunction({
        name: 'user',
        data: {
          type: 'findUserByOpenId',
          user: that.data.user
        }
      }).then(res => {
        if (res.result.data.length<=0) {
          that.data.user.user_type =that.data.user.open_id === 'oYAHL65-h9DwU6EIimiLis8PTFBo' ? '超级管理员' : '普通用户'

          wx.cloud.callFunction({
            name: 'user',
            data: {
              type: 'addUser',
              user: that.data.user
            }
          })
          
        }
        that.data.user.user_type =that.data.user.open_id === 'oYAHL65-h9DwU6EIimiLis8PTFBo' ? '超级管理员' : '普通用户'
        
        //调用数据库保存和本地存储
        wx.setStorageSync('user', JSON.stringify(that.data.user))

        that.setData({
            user:that.data.user
          })
      })
     


      that.setData({
        show: false,
        user_show: true
      });
    }).catch((e) => {
      console.log(e, '==========e')
    });

  },


  logout() {
    this.setData({
      user: {
        avatar_url: defaultAvatarUrl
      },
      show: false,
      user_show: false
    })
    wx.removeStorageSync('user')

  },

  scanCode: function () {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res.result);
        const user = wx.getStorageSync('user')
        const obj_user = JSON.parse(user)
        card.doc(res.result).update({
          data: {
            type: 1,
            write_off_user_id: obj_user.open_id
          }
        }).then(res => {
          console.log(res)
        })
        // 在这里可以处理扫描到的结果，比如发送请求进行核销操作
        wx.showToast({
          title: '核销成功',
          icon: 'success',
          duration: 2000
        });
      },
      fail(err) {
        console.error(err);
        // 处理扫描失败的情况
        // wx.showToast({
        //   title: '核销失败',
        //   icon: 'none',
        //   duration: 2000
        // });
      }
    });
  },
})